﻿
CREATE        FUNCTION Sold(@FunctieCont varchar(1), @debit decimal(20, 2), @credit decimal(20, 2))
RETURNS decimal(20, 2)
AS
BEGIN
	declare @sold as decimal(20, 2)
	set @sold = 0
	if @functieCont = 'A' set @sold = COALESCE(@debit, 0) - COALESCE(@credit, 0)
	else
	if @functieCont = 'P' set @sold = COALESCE(@credit, 0) - COALESCE(@debit, 0)
	else
	if (@functieCont = 'B') OR (@functieCont = 'X') set @sold = abs(COALESCE(@debit, 0) - COALESCE(@credit, 0))

	return @sold
END